package com.ec50n9.server.auth

import cn.dev33.satoken.stp.StpInterface
import com.ec50n9.server.module.resource.ResourceService
import com.ec50n9.server.module.role.RoleService
import com.ec50n9.server.module.user.UserService
import org.springframework.stereotype.Component

@Component
class StpInterfaceImpl(
    private val userService: UserService,
    private val roleService: RoleService,
    private val resourceService: ResourceService
) : StpInterface {
    /**
     * 返回指定账号id所拥有的权限码集合
     *
     * @param loginId  账号id
     * @param loginType 账号类型
     * @return 该账号id具有的权限码集合
     */
    override fun getPermissionList(loginId: Any, loginType: String?): List<String> {
        val permissionList = mutableListOf<String>()
        userService.findOneByUsername(loginId as String).roles.map { role ->
            role.resources.map {
                permissionList.add(
                    "${it.method.lowercase()}===${it.uri}"
                )
            }
        }
        return permissionList
    }

    /**
     * 返回指定账号id所拥有的角色标识集合
     *
     * @param loginId  账号id
     * @param loginType 账号类型
     * @return 该账号id具有的角色标识集合
     */
    override fun getRoleList(loginId: Any?, loginType: String?): List<String> =
        userService.findOneByUsername(loginId as String).roles.map { it.code }
}